package com.ruoyi.dataserver.controller;

import com.ruoyi.dataserver.api.dto.StatisticDto;
import com.ruoyi.dataserver.api.dto.StatisticResultDTO;
import com.ruoyi.dataserver.config.ScConfigAddress;
import com.ruoyi.dataserver.service.IStatisticsService;
import com.ruoyi.dataserver.utils.CommonConstants;
import com.ruoyi.dataserver.utils.ExcelExportUtils;
import com.ruoyi.dataserver.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @ProjectName intelligent-city
 * @Package com.lab.dataserver.controller
 * @ClassName StatisticController
 * @Description 区域统计
 * @Company LAB
 * @Author LXK
 * @Date 2020/6/19 9:39
 * @UpdateUser
 * @UpdateDate
 * @UpdateRemark
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/statistic")
@Slf4j
public class PointStatisticController {

    @Autowired
    private IStatisticsService statisticsService;

    @Autowired
    private ScConfigAddress scConfigAddress;
    /**
     * @param statisticDto :
     * @return com.lab.dataserver.utils.R
     * @Title manholeCountStatistics
     * @Description 井盖数量统计
     * @author heff
     * @date 2020/7/13 15:38
     * @UpdateUser
     * @UpdateDate
     * @UpdateRemark
     */
    @PostMapping("/manholeCount")
    public R manholeCountStatistics(@RequestBody StatisticDto statisticDto, HttpServletResponse response) throws IOException {
        if (statisticDto != null) {
            Map<String, String[]> queryCondition = new HashMap<>();
            queryCondition.put("fsw",new String[]{"井","手孔","人孔","篦子"});
            statisticDto.setQueryCondition(queryCondition);
            statisticDto.setGroupBy("fsw");
            StatisticResultDTO statisticResultDTO = statisticsService.manholeCountStatistics(statisticDto);
            if(statisticDto.getExport()!=null&&statisticDto.getExport().equals(CommonConstants.EXPORT)) {
                String deviceTemplate = scConfigAddress.getExcelExportFilePath();
                ExcelExportUtils excelExportUtils = new ExcelExportUtils();
                excelExportUtils.countUtil(deviceTemplate,statisticResultDTO, response);
            }
            return R.ok(statisticResultDTO, "井盖数量统计结果");
        }
        return R.ok("未查询到结果");
    }

    /**
     * @param statisticDto :
     * @return com.lab.dataserver.utils.R
     * @Title valveCountStatistics
     * @Description 阀门数量统计
     * @author heff
     * @date 2020/7/13 23:05
     */
    @PostMapping("/valveCount")
    public R valveCountStatistics(@RequestBody StatisticDto statisticDto,HttpServletResponse response) throws IOException {
        if (statisticDto != null) {
            Map<String, String[]> queryCondition = new HashMap<>();
            queryCondition.put("fsw",new String[]{"阀门"});
            statisticDto.setQueryCondition(queryCondition);
            statisticDto.setGroupBy("fsw");

            StatisticResultDTO statisticResultDTO = statisticsService.valveCountStatistics(statisticDto);
            if(statisticDto.getExport()!=null&&statisticDto.getExport().equals(CommonConstants.EXPORT)) {
                String deviceTemplate = scConfigAddress.getExcelExportFilePath();
                ExcelExportUtils excelExportUtils = new ExcelExportUtils();
                excelExportUtils.countUtil(deviceTemplate,statisticResultDTO, response);
            }
            return R.ok(statisticResultDTO, "阀门数量统计结果");
        }
        return R.ok("未查询到结果");
    }

    /**
     * @param statisticDto :
     * @return com.lab.dataserver.utils.R
     * @Title materialStatistics
     * @Description 材质统计
     * @author heff
     * @date 2020/7/13 23:07
     */
    @PostMapping("/material")
    public R materialStatistics(@RequestBody StatisticDto statisticDto,HttpServletResponse response) throws IOException {
        if (statisticDto != null) {
            statisticDto.setGroupBy("cz");
            StatisticResultDTO statisticResultDTO = statisticsService.materialStatistics(statisticDto);
            if(statisticDto.getExport()!=null&&statisticDto.getExport().equals(CommonConstants.EXPORT)) {
                String deviceTemplate = scConfigAddress.getExcelExportFilePath();
                ExcelExportUtils excelExportUtils = new ExcelExportUtils();
                excelExportUtils.lengthUtil(deviceTemplate,statisticResultDTO, response);
            }
            return R.ok(statisticResultDTO, "材质统计结果");
        }
        return R.ok("未查询到结果");
    }

    /**
     * @param statisticDto :
     * @return com.lab.dataserver.utils.R
     * @Title accessoriesStatistics
     * @Description 附属物分类统计
     * @author heff
     * @date 2020/7/13 23:10
     */
    @PostMapping("/accessories")
    public R accessoriesStatistics(@RequestBody StatisticDto statisticDto,HttpServletResponse response) throws IOException {
        if (statisticDto != null) {
            statisticDto.setGroupBy("fsw");
            // where trim(t.FSW) is not null
            Map<String, String[]> queryCondition = new HashMap<>();
            queryCondition.put("fsw",new String[]{"Trim"});
            statisticDto.setQueryCondition(queryCondition);
            StatisticResultDTO statisticResultDTO = statisticsService.accessoriesStatistics(statisticDto);
            if(statisticDto.getExport()!=null&&statisticDto.getExport().equals(CommonConstants.EXPORT)) {
                String deviceTemplate = scConfigAddress.getExcelExportFilePath();
                ExcelExportUtils excelExportUtils = new ExcelExportUtils();
                excelExportUtils.countUtil(deviceTemplate,statisticResultDTO, response);
            }
            return R.ok(statisticResultDTO, "附属物分类统计结果");
        }
        return R.ok("未查询到结果");
    }
}